package com.rockxin.cat.fragment;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.rockxin.cat.R;
import com.rockxin.cat.adapter.CapitalLogAdapter;
import com.rockxin.cat.base.BaseFragment;
import com.rockxin.cat.base.Constant;
import com.rockxin.cat.bean.CapitalLogBean;
import com.rockxin.cat.network.CatNetWork;
import com.rockxin.cat.network.HttpResult;
import com.rockxin.cat.utils.SPUtils;

import java.util.ArrayList;
import java.util.List;

import rx.Subscriber;

/**
 * A simple {@link Fragment} subclass.
 * 出借记录
 */
public class CapitalLogFragment extends BaseFragment {

    private CapitalLogAdapter mCapitalLogAdapter;
    private List<CapitalLogBean> capitalLogBeanList;

    private int type;           //1：充值，8：申请提现  5：回款  28：投资奖励

    public CapitalLogFragment() {
    }

    public static CapitalLogFragment newInstance(int type) {
        Bundle args = new Bundle();
        args.putInt("type", type);
        CapitalLogFragment fragment = new CapitalLogFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            type = getArguments().getInt("type");
            capitalLogBeanList = new ArrayList<>();
            SPUtils spUtils = new SPUtils("Guide");
            String token = spUtils.getString(Constant.PREFERENCES.TOKEN);
            final CatNetWork catNetWork = new CatNetWork();
            catNetWork.myLog(type, token, 1, new Subscriber<HttpResult<List<CapitalLogBean>>>() {
                @Override
                public void onCompleted() {

                }

                @Override
                public void onError(Throwable e) {

                }

                @Override
                public void onNext(HttpResult<List<CapitalLogBean>> listHttpResult) {
                    capitalLogBeanList.addAll(listHttpResult.getData());
                    mCapitalLogAdapter.notifyDataSetChanged();
                }
            });
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View mView = inflater.inflate(R.layout.fragment_capital_log, container, false);
        initView(mView);
        return mView;
    }

    private void initView(View mView) {
        RecyclerView recyclerView = mView.findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false));
        mCapitalLogAdapter = new CapitalLogAdapter(capitalLogBeanList);
        recyclerView.setAdapter(mCapitalLogAdapter);
        initHeader();
    }

    private void initHeader() {
        final View inflate = View.inflate(mContext, R.layout.fragment_capital_log_header, null);
        mCapitalLogAdapter.addHeaderView(inflate);
    }

}
